package com.ruoyi.operation.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.http.HtmlUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.operation.domain.bo.TOperationdailyBo;
import com.ruoyi.operation.domain.vo.TOperationdailyVo;
import com.ruoyi.operation.service.ITOperationdailyService;
import com.ruoyi.operation.service.impl.TOperationdailyServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.operation.domain.vo.TOperationdailyResultVo;
import com.ruoyi.operation.domain.bo.TOperationdailyResultBo;
import com.ruoyi.operation.service.ITOperationdailyResultService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 运营部门日志记录结果控制器
 * 前端访问路由地址为:/operation/operationdailyResult
 *
 * @author ruoyi
 * @date 2025-10-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/operationdailyResult")
public class TOperationdailyResultController extends BaseController {

    private final ITOperationdailyResultService iTOperationdailyResultService;
    private final ITOperationdailyService itOperationdailyService;

    /**
     * 查询运营部门日志记录结果列表
     */
    @SaCheckPermission("operation:operationdailyResult:list")
    @GetMapping("/list")
    public List<TOperationdailyVo> list(TOperationdailyBo bo) {
        return itOperationdailyService.getTodaysituation(bo);
    }

    /**
     * 导出运营部门日志记录结果列表
     */
    @SaCheckPermission("operation:operationdailyResult:export")
    @Log(title = "运营部门日志记录结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TOperationdailyBo bo, HttpServletResponse response) {
        List<TOperationdailyVo> todaysituation = itOperationdailyService.getTodaysituation(bo)
            .stream()
            .map(x->{
                String content = HtmlUtil.cleanHtmlTag(x.getContent());
                x.setContent(content);
                return x;
            }).collect(Collectors.toList());
        ExcelUtil.exportExcel(todaysituation, "运营部门日志记录结果", TOperationdailyVo.class, response);
    }

    /**
     * 获取运营部门日志记录结果详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("operation:operationdailyResult:query")
    @GetMapping("/{id}")
    public R<TOperationdailyResultVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iTOperationdailyResultService.queryById(id));
    }

    /**
     * 新增运营部门日志记录结果
     */
    @SaCheckPermission("operation:operationdailyResult:add")
    @Log(title = "运营部门日志记录结果", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TOperationdailyResultBo bo) {
        return toAjax(iTOperationdailyResultService.insertByBo(bo));
    }

    /**
     * 修改运营部门日志记录结果
     */
    @SaCheckPermission("operation:operationdailyResult:edit")
    @Log(title = "运营部门日志记录结果", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TOperationdailyResultBo bo) {
        return toAjax(iTOperationdailyResultService.updateByBo(bo));
    }

    /**
     * 删除运营部门日志记录结果
     *
     * @param ids 主键串
     */
    @SaCheckPermission("operation:operationdailyResult:remove")
    @Log(title = "运营部门日志记录结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iTOperationdailyResultService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @PostMapping("/generate")
    @SaCheckPermission("operation:operationdailyResult:generate")
    @Log(title = "运营部门日志记录生成", businessType = BusinessType.INSERT)
    public R<Void> generate(@RequestBody TOperationdailyResultBo bo) {
        Boolean generate = iTOperationdailyResultService.generate(bo);
        return generate?R.ok("生成成功"):R.fail("无需重复生成数据");
    }

    @PostMapping("/finishWork/{ids}")
    @Log(title = "运营部门日志记录完成", businessType = BusinessType.UPDATE)
    public R<String> finishWork(@PathVariable String[] ids) {
        return iTOperationdailyResultService.finishWork(Arrays.asList(ids));
    }
    @PostMapping("/failWork")
    @Log(title = "运营部门日志记录失败", businessType = BusinessType.UPDATE)
    public R<Void> failWork(@RequestBody TOperationdailyResultBo bo) {
        return toAjax(iTOperationdailyResultService.failWork(bo));
    }
}
